package com.xzl.xep.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.service.Order;
import com.xzl.xep.service.OrderService;
import com.xzl.xep.service.ReportService;


@Controller
public class HomeController extends BaseController{
	
	@Autowired
	private ReportService report;
	
	@Autowired
	private OrderService service;
	

	@RequestMapping("/")
	public String home(Model model) {
		// 转到登录页面
		return toMain(model);
	}

	@RequestMapping("/main")
	public String main(Model model) {
		return toMain(model);
	}

	private String toMain(Model model){
		
		model.addAttribute("today", report.getTodayDto(companyId));
		model.addAttribute("total", report.getTotalDto(companyId));
		model.addAttribute("sales",report.getTotalSales(companyId));
		
		
		return "main";
	}
   
	
	
	@RequestMapping(value ="/order/details/{saleId}" )
	public String orderDetail(@PathVariable Long saleId,Model model) {
		
		model.addAttribute("sale", service.getSale(saleId));
		return "order/orderDetail";
	}
	
	@RequestMapping(value ="/order/add", method=RequestMethod.GET )
	public String addOrder() {
		
		return "order/orderForm";
	}
	
	@RequestMapping(value ="/credit/add", method=RequestMethod.GET )
	public String addCredit() {
		
		return "finance/addCredit";
	}
	
	
	
	@RequestMapping(value ="/order/add", method=RequestMethod.POST )
	@ResponseBody
	public Map<String,Object> sale(Order order) {
		Map<String,Object> result = new HashMap<String,Object>();
		
		result.put("sale", service.sale(order,operator));
		result.put("todaySpend", report.getSpend(companyId, DateUtil.getCurrentDate(), DateUtil.getCurrentDate()));
		result.put("totalSpend", report.getSpend(companyId, null, null));
		result.put("totalCredit", report.getCredit(companyId, null, null));
		result.put("todayCredit", report.getCredit(companyId, DateUtil.getCurrentDate(), DateUtil.getCurrentDate()));
		
		return result;
	}
}
